<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>
<body>

</body>
</html>
<script>
// 案例：求 数组所有元素之和

const arr = [19, 18, 99, 12, 21, 1, 3];

/* 方法1：用普通的 循环方式
  let result = 0;
  arr.forEach(data => {
    result += data;
  });
  console.log(result);
*/

// 方法2：reduce 不用 初始值
const sum1 = arr.reduce((accu, current) => accu + current);

// 方法3：reduce 使用初始值
const sum2 = arr.reduce((accu, current) => accu + current, 0);
console.log(sum1, sum2);

</script>